package com.zhitong.money.mapper;

import com.zhitong.money.entity.Dept;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DeptMapper {
    @Results(id = "deptMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "startTime",property = "startTime"),
            @Result(column = "endTime",property = "endTime"),
            @Result(column = "description",property = "description"),
            @Result(column = "status",property = "status"),
    })
    @Select("select * from dept")
    public List<Dept> findAllDept();

    @ResultMap("deptMap")
    @Select({
            "<script>",
            "select * from dept",
            "<where>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='status!=null and status!=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Dept> findDeptByMap(Map<String,Object> map);


    /**
     * 根据id查询部门
     * @param id 部门id
     * @return
     */
    @ResultMap(value = "deptMap")
    @Select("select * from dept where id =#{id} ")
    public Dept findDeptById(String id);

    /**
     * 根据name查询部门
     * @param name 部门name
     * @return
     */
    @ResultMap(value = "deptMap")
    @Select("select * from dept where name =#{name} ")
    public Dept findDeptByName(String name);

    /**
     *@Author: AndyZhang
     *@Description 根据员工编号获取部门
     *@param
     *@return
     *@Date: 15:45 2021/2/20
     */
    @ResultMap("deptMap")
    @Select("select * from dept where id in (select did from position where id in (select pid from employee where tellerId = #{tid}))")
    public Dept findDeptByTid(String tid);

    /**
     *根据id删除部门
     * @param id
     */
    @ResultMap(value = "deptMap")
    @Delete("delete from dept where id = #{id}")
    public int delDeptById(String id);

    /**
     *根据id删除部门
     * @param ids
     */
    @ResultMap(value = "deptMap")
    @Delete({
            "<script>",
            "delete from dept",
            "where id in",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')' index=''>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int delDeptByIds(List<String> ids);

    /**
     *修改部门详情
     * @param
     */
    @ResultMap(value = "deptMap")
    @Update({
            "<script>",
            "update dept",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='name!=null and name!=\"\"'> name=#{name},</if>",
            "<if test='startTime!=null '> startTime=#{startTime},</if>",
            "<if test='endTime!=null '> endTime=#{endTime},</if>",
            "<if test='description!=null and description!=\"\"'> description=#{description},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void  updateDept(Map<String,Object> map);

    /**
     * 新增部门
     * @param map
     */
    @ResultMap(value = "deptMap")
    @Insert("insert into dept values(#{id},#{name},#{startTime},#{endTime},#{description},#{status})")
    public void addDept(Map<String,Object> map);
}
